package com.itwk.gulimall.product.controller;

import com.itwk.common.utils.PageUtils;
import com.itwk.common.utils.R;
import com.itwk.gulimall.product.entity.PmsCategoryEntity;
import com.itwk.gulimall.product.service.PmsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品三级分类
 *
 * @author wangke
 * @email wangke19970512@163.com
 * @date 2021-01-07 23:03:03
 */
@RestController
@RequestMapping("product/category")
public class PmsCategoryController {
    @Autowired
    private PmsCategoryService pmsCategoryService;
    //压力测试一级菜单
    @GetMapping("/level1")
    public R getCategory1(){
        List<PmsCategoryEntity> level1 = pmsCategoryService.getLevel1();
      return R.ok().put("data", level1);
    }

    //树形父级菜单
    @GetMapping("/list/tree")
    public R listWithTree(){
        List<PmsCategoryEntity> categoryTree = pmsCategoryService.getCategoryTree();

        return R.ok().put("data",categoryTree);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
  //  @RequiresPermissions("product:pmscategory:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = pmsCategoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 修改前回显信息
     */
    @RequestMapping("/info/{catId}")
  //  @RequiresPermissions("product:pmscategory:info")
    public R info(@PathVariable("catId") Long catId){
		PmsCategoryEntity pmsCategory = pmsCategoryService.getById(catId);

        return R.ok().put("data", pmsCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
  //  @RequiresPermissions("product:pmscategory:save")
    public R save(@RequestBody PmsCategoryEntity pmsCategory){
		pmsCategoryService.save(pmsCategory);

        return R.ok();
    }
/*
* 拖拽修改层级，前端会将数据收集，后端直接修改
* */
    @RequestMapping("/update/sort")
    public R updateSort(@RequestBody PmsCategoryEntity[] pmsCategorys){
        pmsCategoryService.updateBatchById(Arrays.asList(pmsCategorys));
        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
  //  @RequiresPermissions("product:pmscategory:update")
    public R update(@RequestBody PmsCategoryEntity pmsCategory){
		pmsCategoryService.updateRelation(pmsCategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:pmscategory:delete")

    public R delete(@RequestBody Long[] catIds){
      /*  删除前需要判断业务逻辑，判断改分类是否被引用，需重写
		pmsCategoryService.removeByIds(Arrays.asList(catIds));
      */
        pmsCategoryService.delCategoryByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
